package cn.xinfei.xdecision.engine.runner.metadata;

import cn.xinfei.xdecision.common.model.configcenter.rejectcode.model.RejectBranchCodeModel;
import cn.xinfei.xdecision.common.model.configcenter.rejectcode.model.RejectCodeModel;
import cn.xinfei.xdecision.common.model.configcenter.rejectcode.model.RejectDiversionLabelModel;
import cn.xinfei.xdecision.common.service.configcenter.RejectBranchCodeService;
import cn.xinfei.xdecision.common.service.configcenter.RejectCodeService;
import cn.xinfei.xdecision.common.service.configcenter.RejectDiversionLabelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RejectCodeLoader {

    @Autowired
    private RejectDiversionLabelService rejectDiversionLabelService;

    @Autowired
    private RejectCodeService rejectCodeService;

    @Autowired
    private RejectBranchCodeService rejectBranchCodeService;


    public Map<Long, RejectBranchCodeModel> init(Long rejectCodeId) {
        log.info("RejectCodeLoader start");
        LambdaQueryWrapper<RejectCodeModel> rejectCodeWrapper = new LambdaQueryWrapper<RejectCodeModel>();
        LambdaQueryWrapper<RejectBranchCodeModel> rejectBranchCodeWrapper = new LambdaQueryWrapper<RejectBranchCodeModel>();
        LambdaQueryWrapper<RejectDiversionLabelModel> rejectDiversionLabelWrapper = new LambdaQueryWrapper<RejectDiversionLabelModel>();
        if (rejectCodeId != null) {
            rejectCodeWrapper.eq(RejectCodeModel::getId, rejectCodeId);
            rejectBranchCodeWrapper.eq(RejectBranchCodeModel::getRejectCodeId, rejectCodeId);
        }

        List<RejectCodeModel> rejectCodeList = rejectCodeService.list(rejectCodeWrapper);
        List<RejectBranchCodeModel> rejectBranchCodeList = rejectBranchCodeService.list(rejectBranchCodeWrapper);
        List<RejectDiversionLabelModel> rejectDiversionLabelList = rejectDiversionLabelService.list(rejectDiversionLabelWrapper);

        Map<Long, RejectCodeModel> rejectCodeMap = rejectCodeList.stream().collect(Collectors.toMap(RejectCodeModel::getId, v -> v));
        Map<Long, RejectBranchCodeModel> rejectBranchCodeMap = rejectBranchCodeList.stream().collect(Collectors.toMap(RejectBranchCodeModel::getId, v -> v));
        Map<Long, RejectDiversionLabelModel> rejectDiversionLabelMap = rejectDiversionLabelList.stream().collect(Collectors.toMap(RejectDiversionLabelModel::getId, v -> v));

        rejectBranchCodeMap.entrySet().stream().forEach(entry -> {
            RejectBranchCodeModel rejectBranchCode = entry.getValue();
            Long rejectCodeId2 = rejectBranchCode.getRejectCodeId();
            Long diversionLabelId = rejectBranchCode.getDiversionLabelId();
            RejectCodeModel rejectCode = rejectCodeMap.get(rejectCodeId2);
            RejectDiversionLabelModel rejectDiversionLabel = rejectDiversionLabelMap.get(diversionLabelId);

            rejectBranchCode.setRejectCode(rejectCode);
            rejectBranchCode.setRejectDiversionLabel(rejectDiversionLabel);

        });

        log.info("RejectCodeLoader end。size={}",rejectBranchCodeMap.size());
        return rejectBranchCodeMap;
    }


}
